<template>
  <el-dialog title="选择部门" width="400px" :visible.sync="params.visible">
    <compTree
      ref="tree"
      :comp-tree-height="'400px'"
      :tree-show-checkbox="params.isMulti"
      :tree-data="treeData"
      :tree-default-checked-keys="treeDefaultCheckedKeys"
    />
    <dialog-btn slot="footer" />
  </el-dialog>
</template>

<script>
/**
 * 入参
 *  title 当前弹窗显示的标题
 *  ids 选择的数据
 *  ismutil 是否多选
 * 出参
 *  name 用户
 *  id 用户id
 * 出参传出方式 this.ok({ name, id })
 */
import { getUserDepartment } from '@/api/user'
import { ParamArr, SQLTable } from '../../../public/js/paramarr'
import Global from '../../../public/js/global'

export default {
  props: {
    params: {
      type: Object,
      default() {
        return {
          title: '',
          // 回显的id
          ids: '',
          // 是否多选
          isMutil: false,
          // 是否可以空选
          isNull: true
        }
      }
    }
  },
  data() {
    return {
      treeData: [],
      treeDefaultCheckedKeys: []
    }
  },
  created() {
    this.initWindow()
  },
  methods: {
    confirm() {
      var ids = []
      var names = []
      if (this.params.isMulti) {
        const nodes = this.$refs.tree.getCheckedNodes()
        for (let i = 0; i < nodes.length; i++) {
          ids.push(nodes[i].id)
          names.push(nodes[i].name)
        }
      } else {
        const node = this.$refs.tree.getCurrentNode()
        if (node) {
          ids.push(node.id)
          names.push(node.name)
        }
      }
      if (ids.length === 0 && !this.params.isNull) {
        return this.$message.warning('请选择部门！')
      }
      this.params.confirm({
        id: ids.join(','),
        name: names.join(',')
      })
    },
    initWindow() {
      getUserDepartment(new ParamArr()).then((vPar) => {
        this.lpfInitWindow(vPar)
      })
    },
    lpfInitWindow(vPar) {
      const nodes = []
      const nodeMap = new Map()
      const table = new SQLTable(vPar.getTable('k_user_department'))
      table.first()
      while (!table.eof()) {
        const node = {}
        const pnode = Global.getTreeviewPNode(
          nodeMap,
          nodes,
          table.getValueByName('PID')
        )
        // data
        node['id'] = table.getValueByName('ID')
        node['name'] = table.getValueByName('Name')
        node['text'] = table.getValueByName('Name')
        node['limitLogin'] = table.getValueByName('LimitLogin')
        node['limitNum'] = table.getValueByName('LimitNum')
        node['type'] = 0
        node['icon'] = 'jiaose'
        node['folder'] = true
        // add
        pnode.push(node)
        nodeMap.set(node.id, node)
        table.next()
      }
      // 回显
      this.treeData = nodes
      if (this.params.ids) {
        if (this.params.isMulti) {
          const idArr = this.params.ids.split(',') || []
          this.treeDefaultCheckedKeys.push(...idArr)
        } else {
          this.$refs.tree.setSelect(this.params.ids)
        }
      }
    }
  }
}
</script>

<style scoped>
</style>
